using Eno.Domain.Repositories;

namespace Eno.Application.Services;

public class AppointPermissionService
{
    private readonly IRoleToPermissionRepository _rep;

    private readonly RoleService _roleService;

    private readonly PermissionService _permissionService;


    public AppointPermissionService(IRoleToPermissionRepository roleToPermissionRepository, RoleService roleService, PermissionService permissionService)
    {
        _rep = roleToPermissionRepository;
        _roleService = roleService;
        _permissionService = permissionService;
    }

    public async Task<int> AppointPermissionToRole(Guid rId, Guid pId)
    {
        // 先查找是否存在该角色和权限
        var role = await _roleService.GetRoleOne(rId);
        var permission = await _permissionService.GetAppPermissionById(pId);
        // 测试数据，实际需要可以修改
        if (role == null || permission == null) return 0;
        return await _rep.AppointPermissionToRole(rId, pId);
    }

    public async Task<int> RemovePermissionInRole(Guid rId)
    {
        // 先查找是否存在该角色
        var role = await _roleService.GetRoleOne(rId);
        if (role == null) return 0;
        return await _rep.RemovePermissionInRole(rId);
    }

    public async Task<int> RemoveSpecificPermissionInRole(Guid roleId, Guid permissionId)
    {
        return await _rep.RemoveSpecificPermissionInRole(roleId, permissionId);
    }

    public async Task<IEnumerable<object>> GetRolePermissionsAsync(Guid roleId)
    {
        // 先查找是否存在该角色
        var role = await _roleService.GetRoleOne(roleId);
        if (role == null) return Enumerable.Empty<object>();
        
        // 获取角色的权限
        var permissions = await _rep.GetRolePermissionsAsync(roleId);
        return permissions;
    }
}